package top.arryweb.application.domain.pojo;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import top.arryweb.application.common.exception.BusinessRuntimeException;
import top.arryweb.application.domain.dto.CreatePaymentOrderDTO;

/**
 * <p>
 * 订单流水表
 * </p>
 *
 * @author Arry
 * @since 2021-05-27
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class BookKeepPaymentOrder implements Serializable {

  private static final long serialVersionUID = 1L;

  @TableId(value = "id", type = IdType.ID_WORKER)
  private Long id;

  private Long createBy;

  /**
   * 创单时间
   */
  @TableField(fill = FieldFill.INSERT) // 新增的时候填充数据
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime createTime;

  private Boolean deleteFlag = false;

  private Boolean enableFlag = true;

  /**
   * 修改时间
   */
  @TableField(fill = FieldFill.INSERT_UPDATE) // 新增或修改的时候填充数据
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime updateTime;

  /**
   * 版本号
   */
  private Long version;

  /**
   * 实付金额
   */
  private BigDecimal actualAmountPaid;

  /**
   * 应付金额
   */
  private BigDecimal amountPayable;

  /**
   * 分账标识，如果分账，该标识为true
   */
  private Boolean distributionFlag;

  /**
   * 商户id，若平台为单商户，可不填写商户id，默认为0
   */
  private Long merchantId;

  /**
   * 支付状态: 1 toBePaid 待支付状态;2 succeed 支付成功;3 partialRefund 部分退款;4 refunded 已退款;5 fail 支付失败;6 revoked
   * 已撤销
   */
  private String payStatus;

  /**
   * 支付时间
   */
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime payTime;

  /**
   * 备注
   */
  private String remark;

  /**
   * 用户id，不可为空
   */
  private Long userId;

  /**
   * 数据校验--创建订单流水ID
   */
  public BookKeepPaymentOrder(CreatePaymentOrderDTO dto) {
    this.createTime = LocalDateTime.now();
    if (dto.getActualAmountPaid().compareTo(dto.getAmountPayable()) > 0) {
      throw new BusinessRuntimeException("实际支付的金额大于待支付的金额");
    }
    this.actualAmountPaid = dto.getActualAmountPaid();
    this.amountPayable = dto.getAmountPayable();
    this.distributionFlag = dto.getDistributionFlag();
    this.merchantId = dto.getMerchantId();
    this.remark = dto.getRemark();
    this.userId = dto.getUserId();
  }

  public BookKeepPaymentOrder setPayTime(LocalDateTime payTime, LocalDateTime createTime) {
    if (createTime.isBefore(payTime)) {
      throw new BusinessRuntimeException("支付的时间应该在创建订单的时间之前");
    }
    this.payTime = payTime;
    return this;
  }
}
